package org.rslai.autotest.core.page.impl;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.rslai.autotest.core.page.AbstractHelper;
import org.rslai.autotest.core.page.Select;

/**
 * Select处理方法
 * @author rslai
 */
public class SelectImpl extends AbstractHelper implements Select {

	@Override
	public String[] getSelectedVisibleTexts(By by) {
		Set<String> set = new HashSet<String>();
		List<WebElement> selectedOptions = new org.openqa.selenium.support.ui.Select(super.driver().findElement(by)).getAllSelectedOptions();
		for (WebElement e : selectedOptions) {
			set.add(e.getText());
		}
		return set.toArray(new String[set.size()]);
	}

	@Override
	public String[] getSelectedValues(By by) {
		Set<String> set = new HashSet<String>();
		List<WebElement> selectedOptions = new org.openqa.selenium.support.ui.Select(super.driver().findElement(by)).getAllSelectedOptions();
		for (WebElement e : selectedOptions) {
			set.add(e.getAttribute("value"));
		}
		return set.toArray(new String[set.size()]);
	}

	@Override
	public String[] getSelectedIndexs(By by) {
		Set<String> set = new HashSet<String>();
		List<WebElement> selectedOptions = new org.openqa.selenium.support.ui.Select(super.driver().findElement(by)).getAllSelectedOptions();
		List<WebElement> options = new org.openqa.selenium.support.ui.Select(super.driver().findElement(by)).getOptions();
		for (WebElement e : selectedOptions) {
			set.add(String.valueOf(options.indexOf(e)));
		}
		return set.toArray(new String[set.size()]);
	}

	@Override
	public String[] getSelectedIds(By by) {
		Set<String> ids = new HashSet<String>();
		List<WebElement> options = new org.openqa.selenium.support.ui.Select(super.driver().findElement(by)).getOptions();
		for (WebElement option : options) {
			if (option.isSelected()) {
				ids.add(option.getAttribute("id"));
			}
		}
		return ids.toArray(new String[ids.size()]);
	}

	@Override
	public String getSelectedVisibleText(By by) {
		return getSelectedOption(by).getText();
	}

	@Override
	public String getSelectedValue(By by) {
		return getSelectedOption(by).getAttribute("value");
	}

	@Override
	public int getSelectedIndex(By by) {
		List<WebElement> options = new org.openqa.selenium.support.ui.Select(super.driver().findElement(by)).getOptions();
		return options.indexOf(getSelectedOption(by));
	}

	@Override
	public String getSelectedId(By by) {
		return getSelectedOption(by).getAttribute("id");
	}

	@Override
	public WebElement getSelectedOption(By by) {
		WebElement selectedOption = null;
		List<WebElement> options = new org.openqa.selenium.support.ui.Select(super.driver().findElement(by)).getOptions();
		for (WebElement option : options) {
			if (option.isSelected()) {
				selectedOption = option;
			}
		}
		return selectedOption;
	}

	@Override
	public boolean isSomethingSelected(By by) {
		List<WebElement> options = new org.openqa.selenium.support.ui.Select(super.driver().findElement(by)).getOptions();
		for (WebElement option : options) {
			if (option.isSelected()) {
				return true;
			}
		}
		return false;
	}

	@Override
	public String[] getSelectOptions(By by) {
		Set<String> set = new HashSet<String>();
		List<WebElement> options = new org.openqa.selenium.support.ui.Select(super.driver().findElement(by)).getOptions();
		for (WebElement e : options) {
			set.add(e.getText());
		}
		return set.toArray(new String[set.size()]);
	}

	@Override
	public void selectByVisibleText(By by, String visibleText) {
		org.openqa.selenium.support.ui.Select select = new org.openqa.selenium.support.ui.Select(super.driver().findElement(by));
		select.selectByVisibleText(visibleText);

		this.sleep(super.conf.getDriverPlaybackSpeed()); // 设置回放速度
	}

	@Override
	public void selectByValue(By by, String optionValue) {
		org.openqa.selenium.support.ui.Select select = new org.openqa.selenium.support.ui.Select(super.driver().findElement(by));
		select.selectByValue(optionValue);

		this.sleep(super.conf.getDriverPlaybackSpeed()); // 设置回放速度
	}

	@Override
	public void selectByIndex(By by, int index) {
		org.openqa.selenium.support.ui.Select select = new org.openqa.selenium.support.ui.Select(super.driver().findElement(by));
		select.selectByIndex(index);

		this.sleep(super.conf.getDriverPlaybackSpeed()); // 设置回放速度
	}

	@Override
	public void addSelectByVisibleText(By by, String visibleText) {
		this.selectByVisibleText(by, visibleText);

		this.sleep(super.conf.getDriverPlaybackSpeed()); // 设置回放速度
	}

	@Override
	public void addSelectByValue(By by, String optionValue) {
		this.selectByValue(by, optionValue);

		this.sleep(super.conf.getDriverPlaybackSpeed()); // 设置回放速度
	}

	@Override
	public void addSelectByIndex(By by, int index) {
		this.selectByIndex(by, index);

		this.sleep(super.conf.getDriverPlaybackSpeed()); // 设置回放速度
	}

	@Override
	public void removeSelectByVisibleText(By by, String visibleText) {
		org.openqa.selenium.support.ui.Select select = new org.openqa.selenium.support.ui.Select(super.driver().findElement(by));
		select.deselectByVisibleText(visibleText);

		this.sleep(super.conf.getDriverPlaybackSpeed()); // 设置回放速度
	}

	@Override
	public void removeSelectByValue(By by, String optionValue) {
		org.openqa.selenium.support.ui.Select select = new org.openqa.selenium.support.ui.Select(super.driver().findElement(by));
		select.deselectByValue(optionValue);

		this.sleep(super.conf.getDriverPlaybackSpeed()); // 设置回放速度
	}

	@Override
	public void removeSelectByIndex(By by, int index) {
		org.openqa.selenium.support.ui.Select select = new org.openqa.selenium.support.ui.Select(super.driver().findElement(by));
		select.deselectByIndex(index);

		this.sleep(super.conf.getDriverPlaybackSpeed()); // 设置回放速度
	}

	@Override
	public void removeAllSelect(By by) {
		org.openqa.selenium.support.ui.Select select = new org.openqa.selenium.support.ui.Select(super.driver().findElement(by));
		select.deselectAll();

		this.sleep(super.conf.getDriverPlaybackSpeed()); // 设置回放速度
	}

}
